class Solution
{
public:
    vector<int> relativeSortArray(vector<int> &arr1, vector<int> &arr2)
    {
        unordered_map<int, int> numIndex;
        for (int i = 0; i < arr2.size(); ++i)
        {
            numIndex[arr2[i]] = i;
        }
        auto comp = [&](int l, int r)
        {
            if (numIndex.count(l) && numIndex.count(r))
            {
                return numIndex[l] < numIndex[r];
            }
            else if (!numIndex.count(l) && !numIndex.count(r))
            {
                return l < r;
            }
            else if (numIndex.count(l))
            {
                return true;
            }
            else
            {
                return false;
            }
        };
        sort(arr1.begin(), arr1.end(), comp);
        return arr1;
    }
};